package com.aaa.ssm.service.Impl;

import com.aaa.ssm.entity.EmpDTO;
import com.aaa.ssm.entity.PermissionDTO;
import com.aaa.ssm.mapper.EmpMapper;
import com.aaa.ssm.mapper.PermissionMapper;
import com.aaa.ssm.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<PermissionDTO> getPermissionsByRid(Integer rid) {
        if (rid.equals(1)) {
            System.out.println(permissionMapper.listSuperAllPermissions());
            return permissionMapper.listSuperAllPermissions();
        } else {
            return permissionMapper.getPermissionsByRid(rid);
        }
    }

    @Override
    public String sendCode(String phone) {
        System.out.println("发送短信");
        try{
            Thread.sleep(1000);
        }catch(InterruptedException e){
            e.printStackTrace();
            throw new RuntimeException("发送短信失败");
        }
      return  "ok";
    }

    @Override
    public EmpDTO login(String username, String password) {
        Long eid = empMapper.login(username, password);
        if (eid==null){
            throw  new RuntimeException("用户名或密码错误");
        }
        EmpDTO emp = empMapper.getEmpInfoByEid(eid);
        Integer state = emp.getState();
        if (state.equals(1)){
            throw  new RuntimeException("该用户已被禁用");
        }else if (state.equals(2)){
            throw  new RuntimeException("员工已经离职");
        }

        return emp;

    }


}